package com.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.entity.HyFurnitureWithImage;
import org.springframework.ui.Model;
import com.baomidou.mybatisplus.plugins.Page;
import com.entity.HyFurniture;
import com.entity.HyFurnitureImage;
import com.service.HyFurnitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 家具列表Controller
 * 
 * @author 白杨
 * @date 2025-02-14
 */
@RestController
@RequestMapping("/furniture")
public class HyFurnitureController
{
    @Autowired
    private HyFurnitureService hyFurnitureService;



    // 添加家具
    @PostMapping("/addFurniture")
    public String addFurniture(@RequestBody HyFurniture hyFurniture) {
        int result = hyFurnitureService.addFurniture(hyFurniture);
        return result > 0 ? "家具添加成功" : "家具添加失败";
    }

    // 删除家具
    @DeleteMapping("/deleteFurniture/{id}")
    public String deleteFurniture(@PathVariable Long id) {
        int result = hyFurnitureService.deleteFurniture(id);
        return result > 0 ? "家具删除成功" : "家具删除失败";
    }

    // 更新家具
    @PutMapping("/updateFurniture")
    public String updateFurniture(@RequestBody HyFurniture hyFurniture) {
        int result = hyFurnitureService.updateFurniture(hyFurniture);
        return result > 0 ? "家具更新成功" : "家具更新失败";
    }



    // 添加家具图片
    @PostMapping("/addFurnitureImage")
    public String addFurnitureImage(@RequestBody HyFurnitureImage hyFurnitureImage) {
        int result = hyFurnitureService.addFurnitureImage(hyFurnitureImage);
        return result > 0 ? "家具图片添加成功" : "家具图片添加失败";
    }

    // 删除家具图片
    @DeleteMapping("/deleteFurnitureImage/{id}")
    public String deleteFurnitureImage(@PathVariable Long id) {
        int result = hyFurnitureService.deleteFurnitureImage(id);
        return result > 0 ? "家具图片删除成功" : "家具图片删除失败";
    }

    // 获取家具图片
    @GetMapping("/getFurnitureImages/{furnitureId}")
    public List<HyFurnitureImage> getFurnitureImages(@PathVariable Long furnitureId) {
        return hyFurnitureService.getFurnitureImages(furnitureId);
    }

    // 根据分类ID获取家具
    @GetMapping("/getFurnitureByCategory/{categoryId}")
    public List<HyFurniture> getFurnitureByCategory(@PathVariable Long categoryId) {
        return hyFurnitureService.getFurnitureByCategory(categoryId);
    }






    //查询家具信息
    @GetMapping("/list")
    public String getFurnitureWithImages(
            Model model,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {

        List<HyFurnitureWithImage> furnitureWithImages = (List<HyFurnitureWithImage>) hyFurnitureService.getFurnitureWithImages(pageNum, pageSize);
        // 将数据传递到模板
        model.addAttribute("furnitureList", furnitureWithImages);
        return "Furnlist";
    }


    //查询所有家具信息
    @GetMapping("/getAllFurniture")
    public List<HyFurniture> getAllFurniture( )
     {
         return hyFurnitureService.getAllFurniture();

     }


    //根据家具id获取家具信息
    @GetMapping("selectByid/{id}")
    public ResponseEntity<HyFurniture> getFurnitureById(@PathVariable Long id) {
        HyFurniture furniture = hyFurnitureService.getFurnitureById(id);
        if (furniture != null) {
            return ResponseEntity.ok(furniture); // 返回成功响应
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); // 返回 404
        }
    }

    //更新库存
    @PutMapping("/updateStock/{furnitureId}/{quantity}")
    public boolean updateStock(@PathVariable Long furnitureId, @PathVariable int quantity) {
        return hyFurnitureService.updateStock(furnitureId, quantity);
    }


     //根据区县id获取该区下所有家具
    @GetMapping("/findFurnitureByRegionId/{categoryId}")
    public List<HyFurniture> findFurnitureByRegionId(@PathVariable Long categoryId)
    {
        return hyFurnitureService.findFurnitureByRegionId(categoryId);
    }



}
